<?php
/**
 * 解析api请求
 */

class ApiRequest {

	private $http_method;
	private $base_uri;
	//request
	private $request_api;
	private $request_api_method;
	private $request_controller;
	private $request_controller_method;
	private $request_format;
	private $request_data;
	private $request_sig;//oauth signature
	private $request_appkey;
	private $request_token;
	private $current_user_id = 0;
	//app
	private $app_id = 0;
	private $app_secret = '';
	private $app_level = 0;
	private $app_source_id;
	private $app_reg_source_id;
	private $app_follow_source_id;
	private $app_is_ip_auth;
	private $app_is_rest_auth;
	private $app_ips;
	private $app_api_list;
	//auth
	private $basic_authrize = '';
	private $basic_sig = '';//basic signature
	private $ip_auth_mobile = '';//ip认证的手机号
	private $ip_auth_uid = '';//ip认证的当前用户
	private $rest_sig = '';//REST签名
	private $ba_auth_mobile = '';//REST认证的手机号
	private $ba_auth_uid = '';//REST认证的当前用户

	//免登录接口
	private $ignore_method = array(
			'oauth2_getauthurl',
			'oauth2_login',
	);

	public function __construct() {
		$this->SetApiAndMethodAndFormat();

		$this->http_method = $_SERVER['REQUEST_METHOD'];
		$this->base_uri = ApiUtility::GetBaseUri();

		$this->request_data = $this->SetInput();

		//oauth2.0
		$this->access_token = isset($this->request_data['token']) ? $this->request_data['token'] : '';
	}

	/**
	 * 验证API请求
	 */
	public function Verify() {
		//检查是否免登录接口
		if (in_array($this->request_api.'_'.$this->request_api_method, $this->ignore_method))
		{
			return ApiErr::API_EC_SUCCESS;
		}

		if (!empty($this->request_data['token'])) //(2) oauth2.0
		{
			return $this->OAuth2Verify();
		}
		else
		{
			throw new Exception(ApiErr::API_EC_PARAM_AUTHORIZE);
		}
	}

	/**
	 * Oauth2.0 token验证
	 */
	private function OAuth2Verify() {
		/*
		$appkey = $uid = '';
		$app_auth_info = App_OAuthorize2::GetDbRowByToken($this->access_token);
		if (!empty($app_auth_info['oauth_token'])){
			$appkey = $app_auth_info['client_id'];
			$uid = $app_auth_info['uid'];
			$expires = $app_auth_info['expires'];
			if ($expires - time() <= 0) {
				throw new Exception(ApiErr::API_EC_APP_ACCESS_TOKEN_EXPIRED);
			}
		} else {
			throw new Exception(ApiErr::API_EC_NOT_AUTHORIZED);
		}
		*/
	}

	/**
	 * 获取请求的参数
	 */
	public function SetInput() {
		return array_merge($_GET, $_POST);
	}

	/**
	 * 获取请求接口类名称
	 */
	public function GetApiController() {
		return $this->request_controller;
	}

	/**
	 * 获取请求接口名称
	 */
	public function GetApiMethod() {
		return $this->request_controller_method;
	}

	/**
	 * 获取请求接口格式
	 */
	public function GetApiEchoFormat() {
		return $this->request_format;
	}

	/**
	 * 获取请接口类及接口方法名，返回类型
	 */
	public function SetApiAndMethodAndFormat(){
		$request_name = '';
		if(!empty($_SERVER['REQUEST_URI'])){
			$request_name = $_SERVER['REQUEST_URI'];
		} elseif(!empty($_SERVER['SCRIPT_NAME'])) {
			$request_name = $_SERVER['SCRIPT_NAME'];
		}

		preg_match("/^\/(\w+)\/(\w+)(\.(json|xml|oauth))?/", $request_name, $matches);

		$this->request_api = $matches[1];
		$this->request_api_method = $matches[2];
		$this->request_format = isset($matches[4]) ? $matches[4] : 'json';

		$this->request_controller = $matches[1];
		$this->request_controller_method = $matches[2];
	}
}
